package com.kun.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.kun.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122676136";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCkSA42A74sTdM0giTsvjdBsyNZh+XPHQ9gpYaW4mWvyjSmctl3PEeQSFGRLf1NCpB0IKyfi6a3lu6JO3BXEO/HZMsArNVTFcvJFByjlOKZLDfHIos0YWHPxgQT2VkBNmXUUe2y1mSfiIzxDZ625O75YybU62v4edO96SFPkqNM6MU4a4XOoRZiHJpXHslSBksnx/NmcLVqy0t3W9O1ahjLjZU0G7Jm7ZPDHd51DRxGo4jFLwoUgFucvzKNfdiUVmBZ15UydgGGDeR+2hixYgaf5HiZ7YepqdHl+cedkz3o4/WI2YSMfsAqyx/mJmNEi6GZ0ul8bg86VpQDqEQiRWpfAgMBAAECggEAGvMTGbtNdDkkH6YF5qxhpi576xEHGw3aWvFsfc4P/mF+d5u5nvzNhJbIgrd3qS7FebduehHPvKhO40ZT7ISDwUMAplC0Ahg+QPNCq3ceISCG2e0JxwL+p7YFLzhliHRjCgySoNbY1O7yqAyi77/okBK66QzhvnZFzQ60fzg//7qfnui/lOTtnEKiU5cksAPoI2zm4lb7bv7qv7njqlghW6nbn5UCTwJmX9zrzcU2DNx7By75gg6SfD2T934LL4syos19mvZrR3RUy4d1qi9r0OV9eg43NyVBF2BudeWAjUfRxFEp6ZkGiFE5PtD/uT9J+zP8oP2Rl55Zu5WwWNGxsQKBgQDviSNuhDh32nL+M/WUbLyV8208U097ZBs8g+56UacmNnbWRewmANDvUmLG7Y2TM/D1g9eFVZEOpUN/2Ynbq3PsFvea5q1Zkl+aPCT+1AR03ReFAexcXay7Yr4g53WRYM0KB4Ba8tHHJIR0DrxWlKQT2VGC+j+D3qY6DbghR5s7lwKBgQCvkr8RCWP2xZE2dLR5kJ9/dBjwkwacTDWsEblrGZ3bIfpOyluaTfMtTdDXB9fVaWhC7CS2LaI1ilo81UoNHIwDZMGJXrasJVBSsXk71CX4DHFpC0jWBLV6+68kW64IqhV3X0Z9FwWrGrZb1UPwG3Ah5Zrb8tSX8zpnS/vONxPAeQKBgBMMuQClC0ic2uBUiYdQTn8uaNWPWpbZk0DwzRp1O/HSCwlnbixnjDY8OxbO9Z9rVhJwdcdo0qxzgDa+ujP6IrH38aEkD008UtYVSmvpuKECpEgzqvR9jqsoYy6Cudrh8+yERDT3cq9/u/ju4xOPpqzdb5oXBM4b+uRLCwW6iCHpAoGBAJvcdvel0XMpUk74bZqZc65OnzE6JB0uXltBZMwZ8pOs5yt44rbpyh4GvYWWtdkDeQYn4jND8YL8Lj1gzxsyo20Dzfen+Bx8B4FRgaFGJOq+CRPKa79qcL0bnxB0vtt8OhA9qTK09FhAH3EIswX9JZ9B2Ca3vFikGM3Im2ImbMdpAoGAR0h0805mBWr0AphnejLMQaGwDYHkk3Sp4UYwazhKtvPjyg0jqDMtWeH9X5vuoiH93anbqC8Qf+ey9Ojs+84Wna4fkJAq3yNsUsmBcNGJ7YOctpJMKh2cxfmOj/bHMD+wriht4LUuVlb5o7UexbbmJ4MPfnbfGX9PTPqBsqlFJzg=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq01jpZnvdsb3ayoiNsPNcCwR2jebL3RZYmsGk2ntGyfFUCUFiCz1TIO5wPeCtMbqEqU14Jfv6x+bPpcPMdMBmTI2XzEFjYBGB4TbZZYQcp50dzg5JyssvfsyJB1hQsiAZP6rVP73O7bi6f8NRDh59SUOqheaCMPteTX1t1N6BrZGHbHUEZMPXChJATzk0emR3pqOS4lr2h+q1PM4mPSecwtUi6ei8Tmhd0hDosP6rdWsWqP/RYhmM4UjOSYjVde6CtFVB4WwGWP+3Tc3Ci0D9tfnpquUe8tHOIycR8CqCkc8QADMt9pXHvv1WYA+Wab3sAGp3TDAoXYmarKAOLdJcQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://localhost:8080/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.mall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
